package com.fmsz.xmly.fragments.findfgts;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.findrecom.FindRecomAdapter;
import com.fmsz.xmly.application.MyApplication;
import com.fmsz.xmly.callbacks.FindRecomHttpService;
import com.fmsz.xmly.callbacks.InterfaceClass;
import com.fmsz.xmly.entitys.findrecom.FindRecomBottomEntity;
import com.fmsz.xmly.entitys.findrecom.FindRecomEntity1;
import com.fmsz.xmly.entitys.findrecom.FindRecomEntity2;
import com.fmsz.xmly.entitys.findrecom.FindRecomEntity3;
import com.fmsz.xmly.entitys.findrecom.FindRecomGuessEntity;
import com.fmsz.xmly.entitys.findrecom.FindRecomSpecialEntity;
import com.fmsz.xmly.entitys.findrecom.GuessChildEntity;
import com.fmsz.xmly.entitys.findrecom.SpecialChildEntity;
import com.fmsz.xmly.entitys.findrecom.VP1ChildEntity;
import com.fmsz.xmly.entitys.findrecom.VP1DataEntity;
import com.fmsz.xmly.entitys.findrecom.VP2ChildEntity;
import com.fmsz.xmly.entitys.findrecom.VP2DataEntity;
import com.fmsz.xmly.fragments.FindFragment;
import com.fmsz.xmly.utils.Urls;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FindRecommendFragment extends Fragment  {
    private InterfaceClass.FindRecomToClassifyCallback callback;
    private FindRecomAdapter adapter;
    private RecyclerView recycler;
    private View rootView;
    private List<VP1DataEntity> vp1List = new ArrayList<>();
    private List<FindRecomGuessEntity> guessList = new ArrayList<>();
    private List<FindRecomGuessEntity> editorList=new ArrayList<>();
    private List<FindRecomSpecialEntity> specialList=new ArrayList<>();
    private List<FindRecomGuessEntity> hotList=new ArrayList<>();
    private List<FindRecomBottomEntity> bottomList=new ArrayList<>();
    private List<VP2ChildEntity> vp2ChildList=new ArrayList<>();

    public FindRecommendFragment() {
        // Required empty public constructor
    }
    public void getInstance(Bundle bundle){
        FindFragment findFragment= (FindFragment) bundle.getSerializable("data");
        callback=findFragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_find__recommend_, container, false);
            recycler = (RecyclerView) rootView.findViewById(R.id.find_recommend_rv);
            adapter = new FindRecomAdapter(getActivity(), vp1List, guessList,editorList,specialList,hotList,bottomList,vp2ChildList,callback);
            recycler.setAdapter(adapter);
            startHttpRequest();
        }
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
    //开启网络数据请求
    private void startHttpRequest() {
        FindRecomHttpService service1 = ((MyApplication) getActivity().getApplication()).getFindRecomService(Urls.FIND_INTERFACE_1_BASE);
        service1.getFindRecomEntity1().enqueue(new Callback<FindRecomEntity1>() {
            //发现-推荐-网络数据请求--1,数据返回
            @Override
            public void onResponse(Call<FindRecomEntity1> call, Response<FindRecomEntity1> response) {
                FindRecomEntity1 body = response.body();
                if (vp1List.size()>0) {vp1List.clear();}
                if (vp2ChildList.size()>0){vp2ChildList.clear();}
                if (editorList.size()>0){editorList.clear();}
                if (specialList.size()>0){specialList.clear();}
                if (body !=null) {
                    List<VP1ChildEntity> vp1ChildList = body.getFocusImages().getList();
                    FindRecomGuessEntity recomEditor = body.getEditorRecommendAlbums();
                    FindRecomSpecialEntity specialColumn = body.getSpecialColumn();
                    for (int i = 0; i < vp1ChildList.size(); i++) {
                        VP1ChildEntity entity = vp1ChildList.get(i);
                        String pic = entity.getPic();
                        String type = entity.getType();
                        if (type.equals("2")){
                            String albumId = entity.getAlbumId();
                            vp1List.add(new VP1DataEntity(pic,type,albumId));
                        }else if (type.equals("3")){
                            String trackId = entity.getTrackId();
                            vp1List.add(new VP1DataEntity(pic,type,trackId));
                        }else if(type.equals("4") || type.equals("10")){
                            String url = entity.getUrl();
                            vp1List.add(new VP1DataEntity(pic,type,url));
                        }else if (type.equals("9")){
                            String specialId = entity.getSpecialId();
                            vp1List.add(new VP1DataEntity(pic,type,specialId));
                        }
                    }
                    List<VP2ChildEntity> list = body.getDiscoveryColumns().getList();
                    vp2ChildList.addAll(list);
                    //小编推荐
                    String editorTitle = recomEditor.getTitle();
                    List<GuessChildEntity> editorChildList = recomEditor.getList();
                    editorList.add(new FindRecomGuessEntity(editorTitle,editorChildList));
                    //精品听单
                    String specialColumnTitle = specialColumn.getTitle();
                    List<SpecialChildEntity> specialColumnList = specialColumn.getList();
                    specialList.add(new FindRecomSpecialEntity(specialColumnTitle,specialColumnList));
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<FindRecomEntity1> call, Throwable t) {}
        });
        service1.getFindRecomEntity2().enqueue(new Callback<FindRecomEntity2>() {
            @Override
            public void onResponse(Call<FindRecomEntity2> call, Response<FindRecomEntity2> response) {
                FindRecomEntity2 body = response.body();
                if (guessList.size()>0){guessList.clear();}
                if (hotList.size()>0){hotList.clear();}
                if (body !=null) {
                    FindRecomGuessEntity guessEntity = body.getGuess();
                    String guessEntityTitle = guessEntity.getTitle();
                    List<GuessChildEntity> guessEntityList = guessEntity.getList();
                    guessList.add(new FindRecomGuessEntity(guessEntityTitle,guessEntityList));

                    List<FindRecomGuessEntity> hotEntitiesList = body.getHotRecommends().getList();
                    hotList.addAll(hotEntitiesList);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<FindRecomEntity2> call, Throwable t) {

            }
        });
        FindRecomHttpService service3 = ((MyApplication) getActivity().getApplication()).getFindRecomService(Urls.FIND_INTERFACE_3_BASE);
        service3.getFindRecomEntity3().enqueue(new Callback<FindRecomEntity3>() {
            @Override
            public void onResponse(Call<FindRecomEntity3> call, Response<FindRecomEntity3> response) {
                FindRecomEntity3 body = response.body();
                if (body!=null) {
                    List<FindRecomBottomEntity> data = body.getData();
                    bottomList.addAll(data);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<FindRecomEntity3> call, Throwable t) {

            }
        });
    }

}
